<?php
namespace Android\Controller;

class AttendController extends BaseController {

    public function submitAttend() {

        $json = urldecode(I ('request.json',''));
        //$json = '{"teachplan_id":1,"timetable_id":4,"value":[{"status":3,"student_id":1}]}';
        //$this->out('RESULT_FAIL',$json);

        if (empty($json))
            $this->out($this->fail,'Empty json');

        // 将获取到得JSON转换为数组
        $jsonArr = json_decode($json,true);

        $data['term_id'] = $this->term['id'];
        $data['teachplan_id'] = $jsonArr['teachplan_id'];
        $data['timetable_id'] = $jsonArr['timetable_id'];
        $data['addtime'] = date('Y-m-d H:i:s');


        // 更改课表状态
        $Timetable = M ('Timetable');
        $Timetable->where('id = '.$data['timetable_id'])->setField('status',1);

        // 将数组信息存入数据库
        $Attend = D ('Attend');
        $count = 0;
        foreach ($jsonArr['value'] as $k=>$v) {

            $data['student_id'] = $v['student_id'];
            $data['status'] = $v['status'];
            $flag = $Attend->addAttend($data);
            $flag !== false && $count++;

        }

        // 返回存储结果
        $this->out($this->success,'提交结果成功, 共有'.$count.'个学生被记录',null);
    }
}